package leetcode.leetcode_104;

import java.util.LinkedList;

class Solution {
    public int maxDepth(TreeNode root) {
        if (root ==null){
            return 0;
        }
        int ans = 0;
        LinkedList<TreeNode> nodes = new LinkedList<>();
        nodes.add(root);
        while(!nodes.isEmpty()){
            int size = nodes.size();
            ans ++;
            while(size -- >0){
                TreeNode poll = nodes.poll();
                if (poll.left != null) {
                    nodes.add(poll.left);
                }
                if (poll.right != null) {
                    nodes.add(poll.right);
                }
            }
        }
        return ans ;
    }
}